import 'package:fish_redux/fish_redux.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:school_safe_teacher/constans/constants.dart';
import 'package:school_safe_teacher/resourse/app_colors.dart';
import 'package:school_safe_teacher/widget/my_widget.dart';
import 'action.dart';
import 'state.dart';

Widget buildView(LoginState state, Dispatch dispatch, ViewService viewService) {
  return getParent(
      child: Container(
        child: ListView(
          padding: EdgeInsets.zero,
          children: [
            Row(
              children: [
                GestureDetector(
                  child: Container(
                    padding: EdgeInsets.fromLTRB(5, 15, 5, 5),
                    child: Image.asset(
                      "images/arrow_left@2x.png",
                      width: 20,
                      height: 20,
                    ),
                  ),
                  onTap: () {
                    Navigator.of(viewService.context).pop(1);
                  },
                ),
                Expanded(
                  child: Text(""), // 中间用Expanded控件
                ),
                // GestureDetector(
                //   child: Container(
                //     padding: EdgeInsets.fromLTRB(5, 15, 5, 5),
                //     child: Text(state.loginWay),
                //   ),
                //   onTap: () {
                //     dispatch(LoginActionCreator.onChangeInputType());
                //   },
                // )
              ],
            ),
            Container(
              margin: EdgeInsets.only(left: 26, top: 22),
              child: Text(
                "欢迎来到校视安",
                style: TextStyle(
                    color: Colors.black,
                    fontSize: 23,
                    fontWeight: FontWeight.bold),
              ),
            ),
            Container(
              margin: EdgeInsets.only(left: 26, top: 16),
              child: Text(
                "关怀等待中的你",
                style: TextStyle(
                    color: Colors.black,
                    fontSize: 14,
                    fontWeight: FontWeight.normal),
              ),
            ),
            Container(
              height: 45,
              margin: EdgeInsets.fromLTRB(25, 50, 25, 0),
              padding: EdgeInsets.only(left: 17, right: 17, top: 5, bottom: 5),
              alignment: Alignment.center,
              width: MediaQuery.of(viewService.context).size.width - 50,
              child: TextField(
                onChanged: (value) {
                  state..mobile = value;
                  dispatch(LoginActionCreator.onInputMobile());
                },
                decoration: InputDecoration(
                    contentPadding: const EdgeInsets.symmetric(vertical: 10.0),
                    icon: Image.asset(
                      state.phoneImg,
                      width: 15,
                      height: 15,
                    ),
                    border: InputBorder.none,
                    hintText: "请输入手机号",
                    isDense: true,
                    hintStyle: TextStyle(
                        fontWeight: FontWeight.normal,
                        fontSize: 14,
                        color: AppColors.hintTextColor)),
                textAlign: TextAlign.start,
                textAlignVertical: TextAlignVertical.center,
              ),
              decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(25),
                  border: Border.all(
                      color: AppColors.editBackgroundColor, width: 1),
                  color: AppColors.editBackgroundColor),
            ),
            pwdOrCode(state, dispatch, viewService),
            Container(
              child: Row(
                children: [
                  Expanded(child: Text("")),
                  GestureDetector(
                    child: Text(
                      "忘记密码",
                      style: TextStyle(
                          fontSize: 13,
                          fontWeight: FontWeight.normal,
                          color: AppColors.middleTextColor),
                    ),
                    onTap: () {},
                  )
                ],
              ),
              padding: EdgeInsets.only(top: 10, right: 36),
            ),
            GestureDetector(
              child: Container(
                height: 45,
                margin: EdgeInsets.fromLTRB(25, 33, 25, 0),
                alignment: Alignment.center,
                width: MediaQuery.of(viewService.context).size.width - 50,
                child: Text(
                  "立即登录",
                  style: TextStyle(
                      color: state.loginTextColor,
                      fontSize: 15,
                      fontWeight: FontWeight.normal),
                ),
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(25),
                  color: AppColors.editBackgroundColor,
                  gradient: LinearGradient(
                    begin: Alignment.centerLeft,
                    end: Alignment.centerRight,
                    colors: [
                      state.loginBtnStartColor,
                      state.loginBtnEndColor,
                    ],
                  ),
                ),
              ),
              onTap: () {
                dispatch(LoginActionCreator.onLogin());
              },
            ),
            _loginService(state, dispatch, viewService)
          ],
        ),
      ),
      context: viewService.context);
}

Widget pwdOrCode(LoginState state, Dispatch dispatch, ViewService viewService) {
  if (state.loginWayType == Constants.LOGIN_CODE_WAY) {
    return Container(
      height: 45,
      margin: EdgeInsets.fromLTRB(25, 13, 25, 0),
      padding: EdgeInsets.only(left: 17, right: 17, top: 5, bottom: 5),
      alignment: Alignment.center,
      width: MediaQuery.of(viewService.context).size.width - 50,
      child: Row(
        children: [
          Expanded(
              child: TextField(
            onChanged: (value) {
              state..code = value;
              dispatch(LoginActionCreator.onInputCode());
            },
            decoration: InputDecoration(
                border: InputBorder.none,
                hintText: "请输入验证码",
                isDense: true,
                hintStyle: TextStyle(
                    fontWeight: FontWeight.normal,
                    fontSize: 14,
                    color: AppColors.hintTextColor)),
            textAlign: TextAlign.start,
            textAlignVertical: TextAlignVertical.center,
          )),
          GestureDetector(
            child: Text(
              state.countdownString,
              style: TextStyle(
                  color: state.textColor,
                  fontSize: 13,
                  fontWeight: FontWeight.normal),
            ),
            onTap: () {
              if (state.countdownTime <= 1) {
                dispatch(LoginActionCreator.onGetCodeClick());
              }
            },
          )
        ],
      ),
      decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(25),
          border: Border.all(color: AppColors.editBackgroundColor, width: 1),
          color: AppColors.editBackgroundColor),
    );
  } else {
    return Container(
      height: 45,
      margin: EdgeInsets.fromLTRB(25, 13, 25, 0),
      padding: EdgeInsets.only(left: 17, right: 17, top: 5, bottom: 5),
      alignment: Alignment.center,
      width: MediaQuery.of(viewService.context).size.width - 50,
      child: TextField(
        onChanged: (value) {
          state..pwd = value;
          dispatch(LoginActionCreator.onInputPwd());
        },
        obscureText: state.showPwd,
        decoration: InputDecoration(
            contentPadding: EdgeInsets.zero,
            icon: Image.asset(
              state.pwdImg,
              width: 15,
              height: 15,
            ),
            border: InputBorder.none,
            suffixIcon: IconButton(
              icon: Image.asset(
                state.pwdCanSeeImg,
                width: 15,
                height: 15,
              ),
              alignment: Alignment.center,
              padding: new EdgeInsets.all(0.0),
              onPressed: () {
                state..showPwd = !state.showPwd;
                dispatch(LoginActionCreator.onShowPwd());
              },
            ),
            hintText: "请输入密码",
            isDense: true,
            hintStyle: TextStyle(
                fontWeight: FontWeight.normal,
                fontSize: 14,
                textBaseline: TextBaseline.alphabetic,
                color: AppColors.hintTextColor)),
        textAlign: TextAlign.start,
        textAlignVertical: TextAlignVertical.center,
      ),
      decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(25),
          border: Border.all(color: AppColors.editBackgroundColor, width: 1),
          color: AppColors.editBackgroundColor),
    );
  }
}

Widget _loginService(
    LoginState state, Dispatch dispatch, ViewService viewService) {
  return Container(
    margin: EdgeInsets.fromLTRB(0, 24, 0, 20),
    child: Row(
      mainAxisAlignment: MainAxisAlignment.center,
      mainAxisSize: MainAxisSize.max,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        GestureDetector(
          child: Container(
            padding: EdgeInsets.fromLTRB(10, 10, 10, 10),
            child: Image.asset(
              state.checkResource,
              width: 14,
              height: 14,
            ),
          ),
          onTap: () {
            dispatch(LoginActionCreator.onCheckAgree());
          },
        ),
        RichText(
            text: TextSpan(
                text: "我已阅读并同意",
                style: TextStyle(
                    fontSize: 10,
                    color: AppColors.smallTextColor,
                    fontWeight: FontWeight.w400),
                children: [
              TextSpan(
                  text: "《校视安》",
                  style: TextStyle(
                      fontSize: 12,
                      fontWeight: FontWeight.w400,
                      color: AppColors.middleTextColor),
                  recognizer: TapGestureRecognizer()
                    ..onTap = () async {
                      //这里做点击事件
                    }),
              TextSpan(
                  text: "及",
                  style: TextStyle(
                      fontSize: 10,
                      fontWeight: FontWeight.w400,
                      color: AppColors.smallTextColor)),
              TextSpan(
                  text: "《校视安隐私政策》",
                  style: TextStyle(
                      fontSize: 12,
                      fontWeight: FontWeight.w400,
                      color: AppColors.middleTextColor),
                  recognizer: TapGestureRecognizer()..onTap = () async {}),
            ]))
      ],
    ),
  );
}
